#import "RNCategoryBrowserCell.h"

CGFloat const RNCategoryBrowserIconPadding = 5.0f;
CGFloat const RNCategoryBrowserIconWidth = 10.0f;
static NSColor *RNCategoryBrowserCheckPathColor;

@implementation RNCategoryBrowserCell

@synthesize displaysIcon = displaysIcon_;
@synthesize checkPath = checkPath_;

+(void)initialize {
    RNCategoryBrowserCheckPathColor = [NSColor colorWithCalibratedWhite:1.0f alpha:1.0f];
}

-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    [super drawWithFrame:[self titleRectForBounds:cellFrame] inView:controlView];
    if ([self displaysIcon])
        [self drawIconWithFrame:[self iconRectForBounds:cellFrame] inView:controlView];
}

-(void)drawIconWithFrame:(NSRect)iconRect inView:(NSView *)controlView {
    NSRect circleRect = NSInsetRect(iconRect, 0.0f, (NSHeight(iconRect) - NSWidth(iconRect)) / 2);
    NSBezierPath *circle = [NSBezierPath bezierPathWithOvalInRect:circleRect];
    [[NSColor darkGrayColor] setFill];
    [circle fill];
    
    NSBezierPath *checkPath = [[self checkPath] copy];
    NSUInteger minDimension = NSHeight(iconRect) < NSWidth(iconRect) ? NSHeight(iconRect) : NSWidth(iconRect);
    CGFloat scaleFactor = minDimension / 100.0f;
    NSAffineTransform *scaleTransform = [NSAffineTransform transform];
    [scaleTransform scaleBy:scaleFactor];
    [checkPath transformUsingAffineTransform:scaleTransform];

    NSAffineTransform *translateTransform = [NSAffineTransform transform];
    [translateTransform translateXBy:iconRect.origin.x yBy:(iconRect.origin.y + (NSHeight([checkPath bounds]) / 2))];
    [checkPath transformUsingAffineTransform:translateTransform];
    
    [RNCategoryBrowserCheckPathColor set];
    [checkPath stroke];
    [checkPath fill];
}

-(NSBezierPath *)checkPath {
    if (!checkPath_) {
        checkPath_ = [NSBezierPath bezierPath];
        [checkPath_ moveToPoint:NSMakePoint(13.0f, 58.0f)];
        [checkPath_ lineToPoint:NSMakePoint(21.0f, 50.0f)];
        [checkPath_ lineToPoint:NSMakePoint(40.0f, 70.0f)];
        [checkPath_ curveToPoint:NSMakePoint(97.0f, 13.0f) controlPoint1:NSMakePoint(49.0f, 49.0f) controlPoint2:NSMakePoint(74.0f, 25.0f)];
        [checkPath_ lineToPoint:NSMakePoint(97.0f, 17.0f)];
        [checkPath_ curveToPoint:NSMakePoint(40.0f, 83.0f) controlPoint1:NSMakePoint(78.0f, 29.0f) controlPoint2:NSMakePoint(53.0f, 53.0f)];
        [checkPath_ closePath];
    }
    return checkPath_;
}

- (NSRect)titleRectForBounds:(NSRect)theRect {
    NSRect titleRect = [super titleRectForBounds:theRect];
    if (![self displaysIcon])
        return titleRect;
    CGFloat iconWidth = RNCategoryBrowserIconWidth + (2 * RNCategoryBrowserIconPadding);
    titleRect.origin.x += iconWidth;
    titleRect.size.width -= iconWidth;
    return titleRect;
}

-(NSRect)iconRectForBounds:(NSRect)bounds {
    NSRect iconRect = bounds;
    iconRect.origin.x += RNCategoryBrowserIconPadding;
    iconRect.size.width = RNCategoryBrowserIconWidth;
    return iconRect;
}

@end
